<?php
/**
* @copyright (C) 2003 - 2008 SystN International Pty Ltd.
* @license http://www.systn.com
* SystN is Free Software
*/

Class Dirver{
	//连接数据库
	function DBLink($dba=''){
		$this->LinkID = ($dba['pconnect'] == 1)? @mssql_pconnect($dba['dbhost'], $dba['dbuser'], $dba['dbpass']) : @mssql_connect($dba['dbhost'], $dba['dbuser'], $dba['dbpass']);
		if (!$this->LinkID){ return 1; }
		
		//选择连接数据库
		if (!@mssql_select_db($dba['dbname'], $this->LinkID)){ return 2; }
		
		return $this->LinkID;
	}
	
	//查询语句
	function query($query,$limit='0') {
		$this -> nums ++;

		//处理最后插入数据ID
		if(eregi("insert into",$query)){
		  $query = $query." SELECT @@IDENTITY as fileId";
		  $isql   = 'Yes';

		}else{
		  $isql   = 'No';
		}
		
		//检测如果有限制数据集则处理
		if($limit>0){
			$query = preg_replace("/SELECT.+?/is","SELECT TOP $limit ",$query);
		}
		
		$query = str_replace("[","",$query);
		$query = str_replace("]","",$query);
		$query = preg_replace("/FROM ([a-z0-9_]+)/i","FROM [\\1]",$query);
		$query = preg_replace("/INSERT INTO ([a-z0-9_]+)/i","INSERT INTO [\\1]",$query);
		$query = preg_replace("/update ([a-z0-9_]+)/i","UPDATE [\\1]",$query);
		$query = preg_replace("/JOIN ([a-z0-9_]+)/i","JOIN [\\1]",$query);
		
		$this-> Lists .= $query.'&nbsp;&nbsp;<font>'.date('Y-m-d H:i:s')."</font><br>";
		
		$querys = @mssql_query($query,$this -> LinkID);
		if($isql=='Yes'){
			$insId = @mssql_fetch_row($querys);
			$this->InsertID=$insId[0];
		}
		
		return $querys;
	}
	
	//返回数组资料
	function fetch_array($query) {
		return @mssql_fetch_array($query);
	}
	
	//取得返回列的数目
	function num_rows($query) {
		return @mssql_num_rows($query);
	}
	
	//返回单列的各字段
	function fetch_row($query) {
		return @mssql_fetch_row($query);
	}
	
	//返回最后一次使用 INSERT 指令的 ID
	function insert_id(){
	  return $this->InsertID;
	}
	
	//检测mssql版本
	function version(){
		return 'SQL Server x';
	}
	
	//关闭数据库
	function close() {
		return @mssql_close($this->LinkId);
	}
	
	//查询语句数
	function sql_num(){
		return $this -> nums;
	}
	
	//查询语句列表
	function sql_debug(){
		return $this-> Lists;
	}
	
	
}
?>